package com.penghaonan.appframework.utils;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;

public class ObjectCache<Key, Value> {
    private Map<Key, SoftReference<Value>> objMap = new HashMap<>();

    public void putCache(Key key, Value value) {
        objMap.put(key, new SoftReference<>(value));
    }

    public Value getCache(Key key) {
        SoftReference<Value> valueRef = objMap.get(key);
        if (valueRef == null) {
            return null;
        }
        Value value = valueRef.get();
        if (value == null) {
            objMap.remove(key);
            return null;
        }
        return value;
    }
}
